#include "enemy.h"
#include<QPixmap>
#include"config.h"
#include<QRandomGenerator>
#include<QGraphicsScene>
#include"player.h"
#include"bullet.h"
#include"score.h"
#include"health.h"
Enemy::Enemy(QGraphicsItem *parent):QGraphicsPixmapItem(parent)
{
    //控制图片大小、刷新频率等
    setPixmap(QPixmap(":/images/bai.png"));
    setScale(PS);
    int max=SH-boundingRect().height()*PS;
    int randomNumber=QRandomGenerator::global()->bounded(1,max);
    setPos(SW-boundingRect().width()*PS,randomNumber);
    startTimer(30);

}


void Enemy::timerEvent(QTimerEvent *)
{
    QList<QGraphicsItem *>itemList=collidingItems();
    for(auto item:itemList){
        if(typeid(*item)==typeid(Player)){
            //健康-1
            Health::getInstance().decrease();
            scene()->removeItem(this);
            delete this;
            return;
        }

        if(typeid(*item)==typeid(Bullet)){
            //加分
            Score::getInstance().increase();
            scene()->removeItem(item);
            scene()->removeItem(this);
            delete item;
            delete this;
            return;
        }
    }

    //位置变化
    setPos(x()-5,y());
    if(x()<0){
        scene()->removeItem(this);
        delete this;
        return;
    }


}
